<?php
/**
 * 商场数据获取
 * @author Ice <iceinto@mallog.com.cn>
 */
class MarketModel extends BaseModel
{
    private $_type = 'market';

    /**
     * 网站首页获取商场列表
     * @return Array 商场列表数据
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getIndexData($map = null, $field = 'id,name,image,initial,cbd', $order = 'm_market.order ASC', $limit = null)
    {
        $data = $this->where($map)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
        return $data;
    }

    /**
     * 获取城市商场数量
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getCityMallCount()
    {
        $map = array();
        $map['city'] = $this->getCity();
        return $this->where($map)->count();
    }

    /**
     * 获取城市商场
     * @param string $limit
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getCityMalls($limit = null)
    {
        $map = array();
        $map['city'] = $this->getCity();
        return $this->getIndexData($map, 'id,name,image,initial', 'm_market.status DESC,m_market.att_count DESC', $limit);
    }

    /**
     * 获取对应城市商圈商场数量
     * @param int $cbd
     * @param int $city
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getCityBusMallCount($cbd = null, $city = null)
    {
        $map = array();
        if ($cbd) {
            $map['cbd'] = $cbd;
        }
        if (!$city) {
            $city = $this->getCity();
        }
        $map['city'] = $city;
        return $this->where($map)->count();
    }

    /**
     * 获取对应城市商圈商场信息
     * @param int $cbd
     * @param string $limit
     * @param int $city
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getCityBusMalls($cbd, $limit = null, $city = null)
    {
        $map = array();
        if ($cbd) {
            $map['cbd'] = $cbd;
        }
        if (!$city) {
            $city = $this->getCity();
        }
        $map['city'] = $city;
        return $this->getIndexData($map, 'id,name,image,initial', 'm_market.status DESC,m_market.att_count DESC', $limit);
    }

    /**
     * 获取商场图片地址
     * @param int $id
     * @return String 图片地址
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getMarketImage($id)
    {
        return $this->getManagedUrl($id, $this->_type);
    }

    /**
     * 获取商场基本信息
     * @param int $id 商场标识
     * @param string $field 获取字段
     * @return array 商场信息
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getById($id, $field = 'id,name,image,initial')
    {
        return $this->field($field)->find($id);
    }

    /**
     * 获取商场基本信息加图片信息
     * @param int $id 商场标识
     * @return array
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getByIdDetailAndImage($id)
    {
        $image_url = null;
        $mall = $this->getById($id);
        if ($mall) {
            $image_url = $this->getMarketImage($mall['image']);
            $mall['image_url'] = $image_url;
        }
        return $mall;
    }
    
    /**
     * 检查商场关注数量是否正确
     * @param int $bid
     * @param int $newCount
     * @param int $attCount
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function attCheckCount($mid, $newCount, $attCount = 0){
    	$isEdit = false;
    	if(empty($attCount)){
    		//获取品牌关注数量
    		$mall = $this->field('att_count')->find($mid);
    		$isEdit = $mall['att_count'] != $newCount ? true : false;
    	}else{
    		$isEdit = $attCount != $newCount ? true : false;
    	}
    	if($isEdit){
    		//更新品牌关注数量
    		$data = array();
    		$data['att_count'] = $newCount;
    		$this->where('id='.$mid)->save($data);
    	}
    }
}

?>